Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

389
Views
trying to include script in pug and call a function from the script, getting "unexpected token" error

I have a pug template where I want to call a function with some variables passed from the endpoint that renders the page:

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    button(type = 'button' onclick='play(#{x},#{y})')
    script(type='text/javascript' src='../public/javascripts/play.js')

Here is play.js:

const play = function(x,y){
    console.log(x+y);
}

When I load the page, I get this error:

Uncaught SyntaxError: Unexpected token ':'     play.js:1

How can I get pug to include the script correctly so that I can call the function with a button click?

EDIT: here's the rendered HTML:

<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<button type="button" onclick="play(#{x},#{y})"></button><script type="text/javascript" src="../public/javascripts/play.js"></script>
</body>
</html>

EDIT 2:

I changed the interpolation to

onclick=`play(${x},${y})`

and this renders as:

onclick="play(2,3)"

which are the values that were passed in from the endpoint, but I'm still getting the unexpected token error.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!